<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="org.teammach.laddercomp.dao.CommentManager"%>
<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="org.teammach.laddercomp.shared.Comment"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.TimeZone"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>


<%@ page import="java.util.List"%>

<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css">
<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-23998388-1']);
 _gaq.push(['_trackPageview']);

 (function() {
   var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
   ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
 })();

</script>
</head>

<body>

<%
	CommentManager manager = CommentManager.INSTANCE;
%>

<% 
	if(request.getParameter("action") != null && request.getParameter("action").compareTo("add")== 0) {
	manager.addComment(request.getParameter("comment"));

	response.sendRedirect("comments.jsp?page=0");		
	}else if (request.getParameter("action") != null && request.getParameter("action").compareTo("delete")== 0) {
		manager.deleteComment(request.getParameter("key"));
		response.sendRedirect("comments.jsp?page=0");		

	}
   
%>

<div id="wrapper">

<a href="/">
<span id="header">
</span>
</a>
<%
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	boolean adminUser = false;
	if (user != null
			&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
					.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
		adminUser = true;
	}
		%>

<div id="page">

<div style="float:left;width:480px">
<div id="content" style="padding:10px;width:460px">
<h1 class="title">User Comments</h1>


<% 
int pageNo = 0;
try{

	pageNo = Integer.parseInt(request.getParameter("page"));
}catch (NumberFormatException nfe) {
	
}
int min = pageNo*10;
int max = (pageNo*10)+9;
List<Comment> comments = manager.getComments(min,max);

if(comments.size() == 0) {
	%>
		There are no comments currently available	
	<%
} else {
	SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mmaaa");
	sdf.setTimeZone(TimeZone.getTimeZone("GMT+10"));
			
	
	for(Comment comment: comments) {
%> 
<h3><%=comment.getUser().getNickname() %></h3>
<%=comment.getContent() %>
<%if(comment.getUser().equals(user)){ %><p><a href="comments.jsp?action=delete&key=<%=KeyFactory.keyToString(comment.getKey()) %>">Delete Comment</a></p>  <%}%>
<p style="text-align:right"><%= sdf.format(comment.getCreated())%></p>
<hr></hr>
<%} %>
	
		<p style="text-align:right">Showing comments <%=min+1 %> to <%=min+comments.size() %>  <%if(comments.size() == 10) { %><a href="comments.jsp?page=<%=(pageNo+1)%>">NEXT PAGE</a> <%} %></p>
		


<div  style="text-align:center;margin-top:-10px;padding:5px">
<% if(user != null) { %>
<FORM METHOD=POST ACTION="comments.jsp">
	<h1 class="title">Add new comment</h1>

	<textarea name="comment" rows="3" cols="60" style="margin-left:10px" ></textarea>
	<input type="hidden" name="action" value="add">
	<P><INPUT TYPE=SUBMIT style="float:right;margin-right:10px">
</FORM> 

<%} else{%>
<p>You must be logged in to leave a comment. <a href="<%=userService.createLoginURL(request.getRequestURI())%>">Login</a></p>
<%} %>
</div>
	<%
	
}
%>

</div>
</div>

<div style="float:right;width:250px">

	
	<div id="sidebar">
<h1 class="title">Links</h1>



	<div  style="text-align:center;margin-top:-10px;padding:5px">
		<ul>
			<li><a href="standings.jsp">Current Standings</a></li>
			<li><a href="rules.jsp">Rules and regulations</a> </li>
			<li><a href="blog.jsp">Haydn's weekly blog</a> </li>
			<li><a href="mailto:dscerri@gmail.com">Contact Webmaster</a> </li>
				<%
		if (user != null
				&& (user.getEmail().equalsIgnoreCase("dscerri@gmail.com") || user
						.getEmail().equalsIgnoreCase("haydns33@gmail.com"))) {
	%>
	<li><a href="<%=userService.createLogoutURL(request.getRequestURI())%>">Log out</a>	</li><%
		} else {
	%>
	<li><a href="<%=userService.createLoginURL(request.getRequestURI())%>">Admin login</a></li>
	<%
		}
	%>
		</ul>
	</div>
	</div>
	</div>
			
	</div>
	
	</div>
	<div id="footer">
	<p>&copy;2011 All Rights Reserved &nbsp;&bull;&nbsp; Design by Dave
	Scerri Productions &nbsp;&bull;&nbsp; CSS Template from <a
		href="http://www.freecsstemplates.org/">Free CSS Templates</a> .</p>
	</div>
</body>
</html>